package com.example.news.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.news.R;
import com.example.news.activity.DetailsActivity;
import com.example.news.bean.TypeDb;
import com.example.news.bean.TypeJson;
import com.example.news.dao.TypeDao;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.ArrayList;
import java.util.Map;
import java.util.Set;

/**
 * Created by Administrator on 2016/5/17.
 */
public class LeftMenuFragment extends Fragment{
    private UMShareAPI umShareAPI;
    private DisplayImageOptions options;
    private TypeDao dao;
    private ArrayList<TypeJson.MyData> list;
    private ListView lv;
    private ArrayList<TypeDb> query;
    private TextView info;
    private ImageView login;
    private ImageView header_logo;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.left_menu, null);

        //找控件
        login = (ImageView) view.findViewById(R.id.QQ_login);
        info = (TextView) view.findViewById(R.id.login_info);
        lv = (ListView) view.findViewById(R.id.menu_list);
        header_logo = (ImageView) getActivity().findViewById(R.id.header_logo);


        dao = new TypeDao(getActivity());

        //第三方登录
        umShareAPI = UMShareAPI.get(getActivity());

        //实现圆形图片
        options = new DisplayImageOptions.Builder().displayer(new RoundedBitmapDisplayer(200)).build();

        //获取网络数据
        getData();

        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SHARE_MEDIA platform = SHARE_MEDIA.QQ;
                umShareAPI.doOauthVerify(getActivity(), platform, umAuthListener);
            }
        });

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), DetailsActivity.class);
                intent.putExtra("title",list.get(position).name);
                startActivity(intent);
            }
        });


        return view;
    }
    private void getData() {
        new Thread() {
            @Override
            public void run() {
                HttpUtils httpUtils = new HttpUtils();
                httpUtils.send(HttpRequest.HttpMethod.GET, "http://169.254.25.59:8080/yidian/json/type.json", new RequestCallBack<String>() {
                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        //连接成功，得到数据
                        String json = responseInfo.result;
                        Gson gson = new Gson();
                        TypeJson typeJson = gson.fromJson(json, TypeJson.class);

                        list = typeJson.data;

                        lv.setAdapter(new MyAadpter());
                    }

                    @Override
                    public void onFailure(HttpException e, String s) {
                        query = dao.query();
                        lv.setAdapter(new BaseAdapter() {
                            @Override
                            public int getCount() {
                                return query.size();
                            }

                            @Override
                            public Object getItem(int position) {
                                return null;
                            }

                            @Override
                            public long getItemId(int position) {
                                return 0;
                            }

                            @Override
                            public View getView(int position, View convertView, ViewGroup parent) {
                                View view = View.inflate(getActivity(), android.R.layout.simple_list_item_1, null);
                                TextView item = (TextView) view.findViewById(android.R.id.text1);
                                item.setTextColor(Color.WHITE);

                                item.setText(query.get(position).getName());
                                return view;
                            }
                        });
                        Toast.makeText(getActivity(), "当前网络不可用", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        }.start();
    }

    /**
     * 自定义适配器
     */
    class MyAadpter extends BaseAdapter {

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = View.inflate(getActivity(), android.R.layout.simple_list_item_1, null);
            TextView item = (TextView) view.findViewById(android.R.id.text1);
            item.setTextColor(Color.WHITE);
            item.setText(list.get(position).name);
            return view;
        }
    }

    /**
     * 第三方登录信息
     */
    private UMAuthListener umAuthListener = new UMAuthListener() {
        @Override
        public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) {
            Toast.makeText(getActivity(), "登录成功", Toast.LENGTH_SHORT).show();
            //获取平台信息
            umShareAPI.getPlatformInfo(getActivity(), platform, new UMAuthListener() {
                @Override
                public void onComplete(SHARE_MEDIA share_media, int i, Map<String, String> map) {

                    Set<String> set = map.keySet();

                    for (String s:set) {
                        String name = map.get("screen_name");
                        String image_url = map.get("profile_image_url");
                        //将获取的用户头像给控件设置上===用户头像
                        ImageLoader.getInstance().displayImage(image_url, login,options);
                        ImageLoader.getInstance().displayImage(image_url, header_logo,options);
                        //用户昵称
                        info.setText(name);

                        //同步程序之间的登录信息
                        FragmentActivity activity = getActivity();
                        MyFragment right = (MyFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.content);
                        right.setInfo(image_url,name);
                    }
                }

                @Override
                public void onCancel(SHARE_MEDIA share_media, int i) {

                }

                @Override
                public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {

                }
            });
        }

        @Override
        public void onError(SHARE_MEDIA platform, int action, Throwable t) {
            Toast.makeText(getActivity(), "登录失败", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onCancel(SHARE_MEDIA platform, int action) {
            Toast.makeText(getActivity(), "已取消登录", Toast.LENGTH_SHORT).show();
        }
    };

    public void setInfo(String url,String name){
        //将获取的用户头像给控件设置上===用户头像
        ImageLoader.getInstance().displayImage(url, login,options);
        ImageLoader.getInstance().displayImage(url, header_logo,options);
        //用户昵称
        info.setText(name);
    }

}
